<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="书山有路勤为径，学海无涯苦作舟！" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    TabLayout  文字/图标/背景动画 |  z阅读
  </title>
  
  <link rel="shortcut icon" href="/zreader/favicon.ico" />
  
  
<link rel="stylesheet" href="/zreader/css/style.css">

  
<script src="/zreader/js/pace.min.js"></script>


  

  

<meta name="generator" content="Hexo 4.2.0"></head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-TabLayout-文字-图标-背景动画" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  TabLayout  文字/图标/背景动画
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/zreader/2020/10/04/TabLayout-%E6%96%87%E5%AD%97-%E5%9B%BE%E6%A0%87-%E8%83%8C%E6%99%AF%E5%8A%A8%E7%94%BB/" class="article-date">
  <time datetime="2020-10-04T03:12:15.000Z" itemprop="datePublished">2020-10-04</time>
</a>
      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h4 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h4><p>在Tablayout的基础上添加字体或图标的缩放及颜色过渡动画等。</p>
<p>使用的Tablayout的版本：com.google.android.material:material:1.2.1</p>
<h5 id="实现效果如下："><a href="#实现效果如下：" class="headerlink" title="实现效果如下："></a>实现效果如下：</h5><img src="https://s1.ax1x.com/2020/10/04/08TUu8.gif" alt="效果图" style="zoom:50%;" />

<h4 id="实现功能："><a href="#实现功能：" class="headerlink" title="实现功能："></a>实现功能：</h4><p>1.修改Indicator动画</p>
<p>2.添加文字缩放及颜色过渡动画</p>
<p>3.添加自定义图标大小及缩放和颜色过渡动画</p>
<p>4.添加背景颜色过渡动画</p>
<h4 id="说明："><a href="#说明：" class="headerlink" title="说明："></a>说明：</h4><p>文中所有代码块中的 //… 代表省略多行代码</p>
<h4 id="准备："><a href="#准备：" class="headerlink" title="准备："></a>准备：</h4><p>从库中复制TabLayout、TabItem，TabLayoutMediator三个类到Tabs库并处理掉错误。</p>
<p>Tabs也是需要附加material库，这里主要是修改import和加@SuppressLint(“RestrictedApi”)注解</p>
<h4 id="1-修改Indicator动画"><a href="#1-修改Indicator动画" class="headerlink" title="1.修改Indicator动画"></a>1.修改Indicator动画</h4><p>分析下改Indicator动画的特点，在页面滑动的前一半Indicator 的宽度由短逐渐变长，滑动的后一半Indicator 的宽度再由长逐渐变短。</p>
<p>我们知道Indicator动画是由SlidingTabIndicator来完成的。首先分析下draw方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">draw</span><span class="params">(@NonNull Canvas canvas)</span> </span>&#123;</span><br><span class="line">     <span class="comment">//...</span></span><br><span class="line">      <span class="comment">// Draw the selection indicator on top of tab item backgrounds</span></span><br><span class="line">      <span class="keyword">if</span> (indicatorLeft &gt;= <span class="number">0</span> &amp;&amp; indicatorRight &gt; indicatorLeft) &#123;</span><br><span class="line">        Drawable selectedIndicator;</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">        selectedIndicator.setBounds(indicatorLeft, indicatorTop, indicatorRight, indicatorBottom);</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">        selectedIndicator.draw(canvas);</span><br><span class="line">      &#125;</span><br><span class="line">	<span class="comment">//...</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>由此看出只要我们按需求修改indicatorLeft和indicatorRight两个成员变量的值就可以实现需要的效果了。</p>
<p>经过一番努力找到</p>
<p> 1.当左右滑动时，onPageScrolled时会调用TabLayout的setScrollPosition方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setScrollPosition</span><span class="params">(<span class="keyword">int</span> position,</span></span></span><br><span class="line"><span class="function"><span class="params">      <span class="keyword">float</span> positionOffset,</span></span></span><br><span class="line"><span class="function"><span class="params">      <span class="keyword">boolean</span> updateSelectedText,</span></span></span><br><span class="line"><span class="function"><span class="params">      <span class="keyword">boolean</span> updateIndicatorPosition)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">    <span class="comment">// Set the indicator position, if enabled</span></span><br><span class="line">    <span class="keyword">if</span> (updateIndicatorPosition) &#123;</span><br><span class="line">      slidingTabIndicator.setIndicatorPositionFromTabPosition(position, positionOffset);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>最终需要在updateIndicatorPosition方法中修改即可。修改如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">updateIndicatorPosition</span><span class="params">()</span> </span>&#123; </span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">    <span class="comment">//下面注释的是原本代码</span></span><br><span class="line">    <span class="comment">//left = (int) (selectionOffset * nextTitleLeft + (1.0f - selectionOffset) * left);</span></span><br><span class="line">    <span class="comment">//right = (int) (selectionOffset * nextTitleRight + (1.0f - selectionOffset) * right);</span></span><br><span class="line">    <span class="keyword">if</span> (selectionOffset &gt;=<span class="number">0.5</span>)&#123;</span><br><span class="line">        left = (<span class="keyword">int</span>) (left + (<span class="number">2</span>*(selectionOffset - <span class="number">0.5f</span>)*(nextTitleLeft - left)));</span><br><span class="line">        right = right+(nextTitleRight - right);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        right = (<span class="keyword">int</span>) (right + <span class="number">2</span>*selectionOffset*(nextTitleRight - right));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>2.当点TabItem切换时，onPageSelected时会调用TabLayout的selectTab方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPageSelected</span><span class="params">(<span class="keyword">final</span> <span class="keyword">int</span> position)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">final</span> TabLayout tabLayout = tabLayoutRef.get();</span><br><span class="line">  <span class="comment">//...</span></span><br><span class="line">    tabLayout.selectTab(tabLayout.getTabAt(position), updateIndicator);</span><br><span class="line"> <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>经animateToTab方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">animateToTab</span><span class="params">(<span class="keyword">int</span> newPosition)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">     <span class="comment">// Set the indicator position, if enabled</span></span><br><span class="line">    <span class="comment">// Now animate the indicator</span></span><br><span class="line">    slidingTabIndicator.animateIndicatorToPosition(newPosition, tabIndicatorAnimationDuration);</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>最终在updateOrRecreateIndicatorAnimation中修改即可。修改如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">updateOrRecreateIndicatorAnimation</span><span class="params">(<span class="keyword">boolean</span> recreateAnimation, <span class="keyword">final</span> <span class="keyword">int</span> position, <span class="keyword">int</span> duration)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">    ValueAnimator.AnimatorUpdateListener updateListener =</span><br><span class="line">          <span class="keyword">new</span> ValueAnimator.AnimatorUpdateListener() &#123;</span><br><span class="line">            <span class="meta">@SuppressLint</span>(<span class="string">"RestrictedApi"</span>)</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onAnimationUpdate</span><span class="params">(@NonNull ValueAnimator valueAnimator)</span> </span>&#123;</span><br><span class="line">              <span class="keyword">final</span> <span class="keyword">float</span> fraction = valueAnimator.getAnimatedFraction();</span><br><span class="line">                <span class="comment">//下面注释的是原本代码</span></span><br><span class="line"><span class="comment">//              setIndicatorPosition(</span></span><br><span class="line"><span class="comment">//                  AnimationUtils.lerp(animationStartLeft, finalTargetLeft, fraction),</span></span><br><span class="line"><span class="comment">//                  AnimationUtils.lerp(animationStartRight, finalTargetRight, fraction));</span></span><br><span class="line">              <span class="keyword">if</span> (finalTargetLeft - animationStartLeft &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (fraction &gt;= <span class="number">0.5f</span>) &#123;</span><br><span class="line">                  <span class="keyword">float</span> tempF = <span class="number">2</span> * (fraction - <span class="number">0.5f</span>);</span><br><span class="line">                  <span class="keyword">int</span> tempL = animationStartLeft + Math.round(tempF * (finalTargetLeft - animationStartLeft));</span><br><span class="line">                  <span class="keyword">int</span> tempR = animationStartRight + Math.round((finalTargetRight - animationStartRight));</span><br><span class="line">                  setIndicatorPosition(tempL, tempR);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                  <span class="keyword">float</span> tempF = <span class="number">2</span> * fraction;</span><br><span class="line">                  <span class="keyword">int</span> tempL = animationStartLeft;</span><br><span class="line">                  <span class="keyword">int</span> tempR = animationStartRight + Math.round(tempF * (finalTargetRight - animationStartRight));</span><br><span class="line">                  setIndicatorPosition(tempL, tempR);</span><br><span class="line">                &#125;</span><br><span class="line">              &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">if</span> (fraction &gt;= <span class="number">0.5f</span>) &#123;</span><br><span class="line">                  <span class="keyword">float</span> tempF = <span class="number">2</span> * (fraction - <span class="number">0.5f</span>);</span><br><span class="line">                  <span class="keyword">int</span> tempL = animationStartLeft + Math.round((finalTargetLeft - animationStartLeft));</span><br><span class="line">                  <span class="keyword">int</span> tempR = animationStartRight + Math.round(tempF*(finalTargetRight - animationStartRight));</span><br><span class="line">                  setIndicatorPosition(tempL, tempR);</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                  <span class="keyword">float</span> tempF = <span class="number">2</span> * fraction;</span><br><span class="line">                  <span class="keyword">int</span> tempL = animationStartLeft + Math.round(tempF * (finalTargetLeft - animationStartLeft));</span><br><span class="line">                  <span class="keyword">int</span> tempR = animationStartRight ;</span><br><span class="line">                  setIndicatorPosition(tempL, tempR);</span><br><span class="line">                &#125;</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>到这里Indicator动画就修改结束了。</p>
<h4 id="2-添加文字缩放及颜色过渡动画"><a href="#2-添加文字缩放及颜色过渡动画" class="headerlink" title="2.添加文字缩放及颜色过渡动画"></a>2.添加文字缩放及颜色过渡动画</h4><p>选中字体和将选中的字体大小及颜色过渡动画。首先在value.xml中添加</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">declare-styleable</span> <span class="attr">name</span>=<span class="string">"TabLayout"</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        text选择时的文字大小 不设置 没有字体缩放动画--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">attr</span> <span class="attr">name</span>=<span class="string">"tabSelectedTextSize"</span> <span class="attr">format</span>=<span class="string">"dimension"</span> /&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        text的默认文字大小 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">attr</span> <span class="attr">name</span>=<span class="string">"tabTextSize"</span> <span class="attr">format</span>=<span class="string">"dimension"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">declare-styleable</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>字体颜色就使用已有的tabSelectedTextColor和tabTextColor。</p>
<p>由于显示的文字是TabView的子View,所以文字缩放及颜色的动画都需要在TabView中完成。</p>
<p>第一步：仿照Indicator动画，在TabView中</p>
<p>1.当左右滑动时 ，添加setPositionFromTabPosition方法</p>
<p>2.当点TabItem切换时，添加animateFromTabPosition方法</p>
<p>由于代码有点多，这里就不在上代码了。文末会附上源码地址</p>
<p>第二步：与Indicator相似分别在相应的位置调用第一步添加的方法。</p>
<p>在setScrollPosition方法中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setScrollPosition</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">      <span class="keyword">int</span> position,</span></span></span><br><span class="line"><span class="function"><span class="params">      <span class="keyword">float</span> positionOffset,</span></span></span><br><span class="line"><span class="function"><span class="params">      <span class="keyword">boolean</span> updateSelectedText,</span></span></span><br><span class="line"><span class="function"><span class="params">      <span class="keyword">boolean</span> updateIndicatorPosition)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">    <span class="comment">// Set the indicator position, if enabled</span></span><br><span class="line">    <span class="keyword">if</span> (updateIndicatorPosition) &#123;</span><br><span class="line">      slidingTabIndicator.setIndicatorPositionFromTabPosition(position, positionOffset);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (updateIndicatorPosition) &#123;</span><br><span class="line">      Tab selectedTempTab = getTabAt(position);</span><br><span class="line">      <span class="keyword">if</span> (selectedTempTab != <span class="keyword">null</span>) &#123;</span><br><span class="line">        selectedTempTab.view.setPositionFromTabPosition(position, positionOffset);</span><br><span class="line">        <span class="keyword">int</span> nextIndex = position + <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> (positionOffset == <span class="number">0</span>) &#123;</span><br><span class="line">          <span class="keyword">if</span> (nextPositionOffset &gt; <span class="number">0.5</span>) &#123;</span><br><span class="line">            nextIndex = position - <span class="number">1</span>;</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            nextIndex = position + <span class="number">1</span>;</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        Tab nextTab = getTabAt(nextIndex);</span><br><span class="line">        <span class="keyword">if</span> (nextTab != <span class="keyword">null</span>) &#123;</span><br><span class="line">          nextTab.view.setPositionFromTabPosition(nextIndex, <span class="number">1f</span> - positionOffset);</span><br><span class="line">        &#125;</span><br><span class="line">        nextPositionOffset = positionOffset;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在animateToTab方法中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">animateToTab</span><span class="params">(<span class="keyword">int</span> newPosition)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">     <span class="comment">// Set the indicator position, if enabled</span></span><br><span class="line">    <span class="comment">// Now animate the indicator</span></span><br><span class="line">    slidingTabIndicator.animateIndicatorToPosition(newPosition, tabIndicatorAnimationDuration);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> (newPosition != getSelectedTabPosition())&#123;</span><br><span class="line">      Tab toTab = getTabAt(newPosition);</span><br><span class="line">      Tab fromTab = getTabAt(getSelectedTabPosition());</span><br><span class="line">      <span class="keyword">if</span> (toTab != <span class="keyword">null</span> &amp;&amp; fromTab != <span class="keyword">null</span>)&#123;</span><br><span class="line">        toTab.view.animateFromTabPosition(<span class="keyword">false</span>,tabIndicatorAnimationDuration);</span><br><span class="line">        fromTab.view.animateFromTabPosition(<span class="keyword">true</span>,tabIndicatorAnimationDuration);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>至此文字的缩放及颜色动画的大致流程就说完了。具体细节如给文字预留一些距离等请查看源码。</p>
<h4 id="3-添加自定义图标大小及缩放和颜色过渡动画"><a href="#3-添加自定义图标大小及缩放和颜色过渡动画" class="headerlink" title="3.添加自定义图标大小及缩放和颜色过渡动画"></a>3.添加自定义图标大小及缩放和颜色过渡动画</h4><p>由于图标的缩放和颜色过渡动画和文字的非常相似，这里就不再叙说。</p>
<p>首先呢，这里说的自定义图标大小是自定义默认的图标大小，即tab.setIcon中的Icon大小。</p>
<p>实现思路：在setIcon时给ImageView设置大小。如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@NonNull</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Tab <span class="title">setIcon</span><span class="params">(@Nullable Drawable icon)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.icon = icon;</span><br><span class="line">  <span class="comment">//...</span></span><br><span class="line">  view.initIconView();</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">initIconView</span><span class="params">()</span></span>&#123;</span><br><span class="line">  LinearLayout.LayoutParams lp = (LayoutParams) iconView.getLayoutParams();</span><br><span class="line">  <span class="keyword">if</span> (tabIconWidth != <span class="number">0</span>) &#123;</span><br><span class="line">    lp.width = tabIconWidth;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> (tabIconHeight != <span class="number">0</span>)&#123;</span><br><span class="line">    lp.height = tabIconHeight;</span><br><span class="line">  &#125;</span><br><span class="line">  iconView.setLayoutParams(lp);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-添加背景颜色过渡动画"><a href="#4-添加背景颜色过渡动画" class="headerlink" title="4.添加背景颜色过渡动画"></a>4.添加背景颜色过渡动画</h4><p>实现方式和文字颜色过渡是相似的，这里就不再重复。</p>
<p>需注意点：</p>
<p>1.原本TabView的背景是设置的是点击水波纹效果，设置背景过渡动画会去除水波纹效果，动画结束需要重新设置点击水波纹效果</p>
<p>2.TabView的背景遮挡Indicator动画。需要把SlidingTabIndicator的draw方法的super.draw放到第一句</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">draw</span><span class="params">(@NonNull Canvas canvas)</span> </span>&#123;</span><br><span class="line">  <span class="comment">// Draw the tab item contents (icon and label) on top of the background + indicator layers</span></span><br><span class="line">  <span class="keyword">super</span>.draw(canvas);</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>至此本文已结束。</p>
<h4 id="特奉上-源码"><a href="#特奉上-源码" class="headerlink" title="特奉上  源码"></a>特奉上  <a href="https://gitee.com/zhufred/tablayout">源码</a></h4><h4 id="该Library应用于小说阅读器-Z阅读"><a href="#该Library应用于小说阅读器-Z阅读" class="headerlink" title="该Library应用于小说阅读器 Z阅读"></a>该Library应用于小说阅读器 <a href="https://zhufred.gitee.io/zreader/">Z阅读</a></h4>
      
      <!-- 打赏 -->
      
        <div id="reward-btn">
          打赏
        </div>
        
    </div>
    <footer class="article-footer">
      <a data-url="https://yukun314.gitee.io/zreader/2020/10/04/TabLayout-%E6%96%87%E5%AD%97-%E5%9B%BE%E6%A0%87-%E8%83%8C%E6%99%AF%E5%8A%A8%E7%94%BB/" data-id="ckpux5kh70000f0cpf8o0angs"
        class="article-share-link">分享</a>
      
    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/zreader/2020/11/01/android%E4%BD%BF%E7%94%A8%E5%8D%8F%E7%A8%8B/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            android使用协程
          
        </div>
      </a>
    
    
      <a href="/zreader/2020/01/16/%E5%8E%86%E5%8F%B2%E4%B8%8A%E7%9A%84%E4%BB%8A%E5%A4%A9API/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">历史上的今天API</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: false,
        app_id: 'nncc5vxkXViaodaRNq6mdmPB-gzGzoHsz',
        app_key: 'BT53GVtaskTdp1jDv0zVWW3u',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '留下您宝贵的意见！',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li style="text-align:center;font-size:1.5em">
        &copy;
        2019-2021
        &nbsp;&nbsp;&nbsp;fred
      </li>
    </ul>
	<p>App使用的所有小说为转载作品，所有章节均由网友上传，App只是为了宣传本书让更多读者欣赏。</p>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    
    <aside class="sidebar">
      
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/zreader/"><img src="/zreader/images/icon.png" alt="z阅读"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/zreader/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/zreader/archives">目录</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
      <a class="nav-item-link" target="_blank" href="/zreader/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>赐予我力量吧</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/zreader/images/zhifubao.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/zreader/images/weixin.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/zreader/js/jquery-2.0.3.min.js"></script>


<script src="/zreader/js/jquery.justifiedGallery.min.js"></script>


<script src="/zreader/js/lazyload.min.js"></script>


<script src="/zreader/js/busuanzi-2.3.pure.min.js"></script>




  
<script src="/zreader/js/tocbot.min.js"></script>

  <script>
    // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
    tocbot.init({
      tocSelector: '.tocbot',
      contentSelector: '.article-entry',
      headingSelector: 'h1, h2, h3, h4, h5, h6',
      hasInnerContainers: true,
      scrollSmooth: true,
      positionFixedSelector: '.tocbot',
      positionFixedClass: 'is-position-fixed',
      fixedSidebarOffset: 'auto',
    });
  </script>


<script>
  var ayerConfig = {
    mathjax: false
  }
</script>


<script src="/zreader/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  
  
  </div>
</body>

</html>